package com.biyabi;

import com.biyabi.pojo.*;
import com.biyabi.service.CartService;
import com.biyabi.service.ProductService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.Map;


@RunWith(SpringRunner.class)
@SpringBootTest
public class CartToRedis {
    @Resource
    private CartService cartService;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private ProductService productService;
    @Test
    public void add(){
        CartItem cartItem = new CartItem();
        //模拟用户选择一个商品一个商品
        ProductPojo one = productService.findOne("008bbf02-8f2f-427b-85a9-d6e8c39a7e7a");
        //根据用户选择的上找到对应的sku id
        SkuPojo skuPojo = one.getSkuPojos().get(0);

        cartItem.setSkuPojo(skuPojo);
        cartItem.setSum(15);
        cartItem.setId("42e39323-3ee4-43ef-b20c-4281b779ff11");
        cartItem.setProduct(one.getProduct());
        cartService.addCartToRedis("1",cartItem);
    }
    @Test
    public void selectAll(){
        Cart cartFromRedis = cartService.getCartFromRedis("1");
        if(cartFromRedis!=null){
            Map<String, CartItem> cartItems = cartFromRedis.getCartItems();
            for (String s : cartItems.keySet()) {
                CartItem cartItem = cartItems.get(s);
                System.out.println(cartItem.getSum());
                System.out.println(cartItem.getProduct().getName());
            }
            System.out.println(cartFromRedis.getTotalPrice());
        }
    }
    @Test
    public void delete(){
        Result result = cartService.delProductFromRedis("1", "42e39323-3ee4-43ef-b20c-4281b779ff11");
        System.out.println(result);

    }
    @Test
    public void createOrderId(){
        redisTemplate.opsForValue().set("OrderId",1000000000);
        //redisTemplate.delete("OrderId");
        System.out.println(redisTemplate.opsForValue().increment("OrderId",1).toString());
    }

}
